package com.lad.springboot_mybatis.service;

import com.lad.springboot_mybatis.entity.User;
import com.lad.springboot_mybatis.mapper.UserMapper;
import com.lad.springboot_mybatis.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:wjup
 * @Date: 2018/9/26 0026
 * @Time: 15:23
 */
@Service
public class UserService {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    UserMapper userMapper;
    @Autowired
    SaveService saveService;

    public User Sel(int id) {
        return userMapper.Sel(id);
    }

    public User selectUserByName(String name) {
        return userMapper.selectUserByName(name);
    }

    public User selectUserByCache(String id) {
        User userCache = redisUtils.getEntity(id, User.class);
        if (userCache != null) {
            return userCache;
        }
        User userDb = userMapper.Sel(Integer.valueOf(id));
        if (userDb != null) {
            redisUtils.putEntity(userDb.getId().toString(), userDb);
        }
        return userDb;
    }

    public void update() {

        Map<String, String> map = new HashMap<>();
        map.put("userName", "zs");
       /* map.put("passWord", "1");
        Map<String, String> map1 = new HashMap<>();
        map1.put("userName", "b");
        map1.put("passWord", "2");
        Map<String, String> map2 = new HashMap<>();
        map2.put("userName", "c");
        map2.put("passWord", "3");*/
        List<Map<String, String>> lists = new ArrayList<>();
        lists.add(map);
//        lists.add(map1);
//        lists.add(map2);
        userMapper.updateUsers(lists);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveA() {
        User user = new User();
        user.setId(1);
        user.setUserName("zs");
        userMapper.insertUser(user);
        saveService.saveB();
//        int i = 1/0;
    }

}